$(function(){

    $('.detail-dialog').click(function(){
        $.ajax({
            url : '/hplus-webapp/tracking/detail',
            data : {id : $(this).attr('product-id')},
            success : function(data) {
                $('#invoice-dialog').html(data);
                $('#invoice-dialog').dialog({
                    dialogClass: "no-close",
                    buttons: [
                        {
                            text: "닫기",
                            click: function() {
                                $( this ).dialog( "close" );
                            }
                        }
                    ],
                    title:'배송상세정보',
                    width:900,
                    height:500
                });
            }
        });
    });


    $('.sortable').click(function(){

        var th = $(this);
        var sort = {};
        sort.name =  th.attr('sort-name');
        sort.direction = th.attr('sort-state') == 'NONE' ? 'DESC' : th.attr('sort-state');


        var form = $('#searchCondition');

        if($('#sortName').length == 0) {

            $('<input>').attr({
                'type' : 'hidden',
                'id' : 'sortName',
                'name' : 'sortName',
                'value' : sort.name
            }).appendTo(form);
        } else {
            $('#sortName').attr('value', sort.name);


        }

        if($('#sortDirection').length == 0) {
            $('<input>').attr({
                'type' : 'hidden',
                'id' : 'sortDirection',
                'name' : 'sortDirection',
                'value' : sort.direction
            }).appendTo(form);
        } else {
            $('#sortDirection').attr('value', sort.direction);
        }

        $.ajax({
            url : '/hplus-webapp/tabledata',
            method : 'GET',
            data : form.serialize(),
            success : function(data) {

                th.parent().children().each(function() {
                    $(this).find('span').attr('class', 'css_right ui-icon ui-icon-carat-2-n-s pointer');
                });
                th.parent().children().each(function() {
                    $(this).attr('sort-state', 'NONE');
                });

                if(sort.direction == 'NONE') {
                    th.attr('sort-state', 'DESC');
                    th.find('span').attr('class', 'css_right ui-icon ui-icon-triangle-1-s pointer');
                } else if(sort.direction == 'ASC') {
                    th.attr('sort-state', 'DESC');
                    th.find('span').attr('class', 'css_right ui-icon ui-icon-triangle-1-n pointer');
                } else if(sort.direction == 'DESC') {
                    th.attr('sort-state', 'ASC');
                    th.find('span').attr('class', 'css_right ui-icon ui-icon-triangle-1-s pointer');
                }

                $('#search-table').find('tbody').remove();
                $('#search-table').append(data);
            }
        });


    })

});